/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { message } from "antd";
import {
  wxGetQrList, // 二维码，业务员列表
  wxGetQrLink, // 二维码，获取查看二维码链接
  wxQrCreateExport, // 二维码，创建导出任务
  wxQrDownLoad, // 二维码，获取导出结果进行下载
  changeQrcodeCourier,
} from "@/services/platform";
import { isLegalData } from "@/utils/utils";

export default {
  namespace: "wxQrCode",

  state: {
    operatorList: {
      list: [],
      paganation: {},
    },
    qrcodeUrl: "",
    qrCodeDownLoadPath: "",
  },

  effects: {
    // 二维码，获取业务员列表
    *wxGetQrList({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const { type } = payload;
      const response = yield call(wxGetQrList, payload);
      if (!response) return;
      const { code, data, msg } = response;
      const { list, total, size, page } = data;

      if (code == 0) {
        if (type == "courier") {
          yield put({
            type: "save",
            payload: {
              operatorList: {
                list: isLegalData(list, []),
                pagination: {
                  total,
                  pageSize: size,
                  page,
                },
              },
            },
          });
        } else {
          yield put({
            type: "save",
            payload: {
              siteList: {
                list: isLegalData(list, []),
                pagination: {
                  total,
                  pageSize: size,
                  page,
                },
              },
            },
          });
        }
        __dva_resolve(response);
      } else {
        __dva_reject(code);
        code != 9999 && message.error(msg);
      }
    },
    // 二维码，获取查看二维码链接
    *wxGetQrLink({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(wxGetQrLink, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        __dva_resolve(data);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 二维码，创建导出任务
    *wxQrCreateExport({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(wxQrCreateExport, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0 && data) {
        __dva_resolve(data);
        message.success(msg);
      } else {
        __dva_reject();
        message.error("创建导出任务失败！");
      }
    },
    // 二维码，获取导出结果进行下载
    *wxQrDownLoad({ payload, __dva_resolve, __dva_reject }, { call, put }) {
      const response = yield call(wxQrDownLoad, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        yield put({
          type: "save",
          payload: {
            qrCodeDownLoadPath: data,
          },
        });
        __dva_resolve(data);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
    // 二维码，修改二维码业务员
    *changeQrcodeCourier({ payload, __dva_resolve, __dva_reject }, { call }) {
      const response = yield call(changeQrcodeCourier, payload);
      if (!response) return;
      const { code, data, msg } = response;

      if (code == 0) {
        __dva_resolve(data);
        message.success(msg);
      } else {
        __dva_reject();
        message.error(msg);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
